package Dao.daoImpl;

import Controller.UserController;
import Dao.empDao;
import Model.Emp;

import java.util.Arrays;

public class EmpDaoImpl implements empDao {
    //删除
    @Override
    public void delEmpDao(Emp[] emp, String empId, int len) {
        boolean flag = false;
        int j = 0;//标记要删除的下标
        //遍历
        for (int i = 0; i < len; i++) {
            if (emp[i].getEmpId() == Integer.valueOf(empId)) {
                j = 1;
                flag = true;
                break;
            }
        }
        //覆盖
        if (flag) {//7
            for (int i = j; i < len - 1; i++) {
                emp[i] = emp[i + 1];
            }
            UserController.num--;//总长度减1；
            System.out.println("删除成功");
        } else {
            System.out.println("未找到");
        }
    }

    //查询
    /*
       @param emp
          @param len
     */
    @Override
    public void showEmpDao(Emp[] emp, int len) {
        System.out.println("工号\t姓名\t性别\t年龄\t工作");
        for (int i = 0; i < len; i++) {
            System.out.println(emp[i].getEmpId() + "\t" +
                    emp[i].getName() + "\t" +
                    emp[i].getSex() + "\t" +
                    emp[i].getAge() + "\t" +
                    emp[i].getJob());
        }
    }
//修改
    @Override
    public int updateEmpDao(Emp[] emp, String empId, int len) {
        for (int i = 0; i < len; i++) {
            if (emp[i].getEmpId()==Integer.valueOf(empId)){
                return i;
            }
        }
        return -1;
    }

    /*增加
              数据访问层 添加员工信息
              @param emp Array 数据集合 地址
              @param user 一条员工信息
           */
    @Override
    public void addEmpDao(Emp[] emp, Emp user, int len) {

        emp[UserController.num++] = user;
        System.out.println("测试len = " + len);
        for (int i = 0; i < len; i++) {
            System.out.println("emp[i]. tostring() =" + emp[i].toString());
        }
        //System.out.println("Array.tostring()=" + Arrays.toString(emp));


    }
}
